package arithmetic.socket.netty.first;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * 源码地址:
 * https://github.com/huiGod/netty_lecture
 * 学习地址:https://pan.baidu.com/s/1IO9gTu0sD7_7pACFet99Nw#list/path=%2F
 *
 * Created on 2020-01-20
 * 最简单服务端
 */
public class NettyServer {

    public static void main(String[] args) throws InterruptedException {
        // 线程组, 接收请求
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        // 处理请求
        EventLoopGroup workGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup, workGroup).channel(NioServerSocketChannel.class)
                    .childHandler(new ServerInitializer());

            // 绑定端口  使用f.channel().closeFuture().sync()方法进行阻塞,等待服务端链路关闭之后main函数才退出
            ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();
            channelFuture.channel().closeFuture().sync();
        } finally {
            // 优雅关闭
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }
    }

}
